import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

void main() {
  runApp(
    const ProviderScope(child: MyApp()),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: CounterPage());
  }
}

//第一个案例
final counterProvider = StateProvider<int>((ref) => 0);

final counterSquaredProvider = Provider<int>((ref) {
  final count = ref.watch(counterProvider);
  return count * count;
});

// 条件Provider - 判断是否为偶数
final isEvenProvider = Provider<bool>((ref) {
  final count = ref.watch(counterProvider);
  return count % 2 == 0;
});

class CounterPage extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // 监听状态变化
    final count = ref.watch(counterProvider);
    final squared = ref.watch(counterSquaredProvider);
    final isEven = ref.watch(isEvenProvider);

    return Scaffold(
      appBar: AppBar(
        title: Text('Riverpod 计数器'),
        backgroundColor: Colors.blue,
        foregroundColor: Colors.white,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 当前计数
            Text(
              '计数: $count',
              style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 16),
            // 平方值
            Text(
              '平方: $squared',
              style: TextStyle(fontSize: 24, color: Colors.blue),
            ),
            SizedBox(height: 16),

            // 奇偶性
            Container(
              padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
              decoration: BoxDecoration(
                color: isEven ? Colors.green : Colors.orange,
                borderRadius: BorderRadius.circular(20),
              ),
              child: Text(
                isEven ? '偶数' : '奇数',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
            SizedBox(height: 32),
            // 操作按钮
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                FloatingActionButton(
                  heroTag: "decrement",
                  onPressed: () => ref.read(counterProvider.notifier).state--,
                  tooltip: '减少',
                  child: Icon(Icons.remove),
                ),
                FloatingActionButton(
                  heroTag: "reset",
                  onPressed: () => ref.read(counterProvider.notifier).state = 0,
                  tooltip: '重置',
                  backgroundColor: Colors.grey,
                  child: Icon(Icons.refresh),
                ),
                FloatingActionButton(
                  heroTag: "increment",
                  onPressed: () => ref.read(counterProvider.notifier).state++,
                  tooltip: '增加',
                  child: Icon(Icons.add),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
